#!/usr/bin/perl -w
use strict;

# This script written in perl creates statistics from Vietcong gameservers.
# author       Bernd Essl <bernd@ak-47.at>
# copyright    Bernd Essl <bernd@ak-47.at>
# license      license   http://gnu.org/copyleft/gpl.html GNU GPL
# version      SVN: 1
# link         http://ak-47.at/code/vc_stats/
# vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4
# utf-8 saved

if (@ARGV < 2)
{
    print "\n++++++++++++++++++++++++++++++\n";
    print "USAGE: perl vc-stats.pl LOGFILE CLANTAG \n";
    print "LOGFILE - name of the logfile - example: big.log \n";
    print "CLANTAG - use backslashes on special chars - example: '\\[PVC\\]' \n";
    print "example CMD: perl vc-stats.pl big.log '\\[PVC\\]' \n\n";
    print "doesn't work? mail me bernd\@ak-47.at\n";
    exit();
}

my $file = $ARGV[0];
my $clan_tag = $ARGV[1];

my %clan_member_atg_kill;
my %clan_member_atg_dths;
my %clan_member_ctf_pnts;
my %clan_member_ctf_kills;
my %clan_member_ctf_dths;
my %clan_member_dm_frags;
my %clan_member_dm_kills;
my %clan_member_dm_dths;

my ($party, $points, $map, $gt);

my $check = 0;
my $res_points_us = 0;
my $res_points_vc = 0;
my $res_gt_dm = 0;
my $res_gt_atg = 0;
my $res_gt_ctf = 0;
my $res_map_arroyo = 0;
my $res_map_bahnar = 0;
my $res_map_dustWorld = 0;
my $res_map_halongPort = 0;
my $res_map_indiancountry = 0;
my $res_map_nva = 0;
my $res_map_waterFall = 0;
my $res_map_stream = 0; 
my $res_map_storm = 0;
my $res_map_frontier = 0;
my $res_map_threeCanyons = 0;
my $res_map_riverDale = 0;
my $res_clan_ctf_pnts = 0;
my $res_clan_ctf_kills = 0;
my $res_clan_ctf_dths = 0;
my $res_clan_dm_frags = 0;
my $res_clan_dm_kills = 0;
my $res_clan_dm_dths = 0;
my $res_clan_atg_kills = 0;
my $res_clan_atg_dths = 0;


sub hashMembers(\%)
{
    my %hashes = %{(shift)};
    my @a_keys = keys %hashes;

    foreach (reverse sort {$hashes{$a} <=> $hashes{$b}} (keys(%hashes)))
    { 
        print "$_ - $hashes{$_}\n"; 
    }
}


open( FILE, "< $file" ) or die "Can't open $file : $!";
while( <FILE> )
{
    my $line = $_;
    
    #how much points kills deaths have my clan
    
    #CTF: pnts: 0     kills: 6    dths: 2
    if ($line =~ /^$clan_tag(.+).+pnts: (\d+).+kills: (\d+).+dths: (\d+)/) #whitespaces fix later
    {
        $res_clan_ctf_pnts = $res_clan_ctf_pnts + $2;
        $res_clan_ctf_kills = $res_clan_ctf_kills + $3;
        $res_clan_ctf_dths = $res_clan_ctf_dths + $4;
    
        #CTF - single user stats pnts
        if (exists $clan_member_ctf_pnts{$1})
        {
            $clan_member_ctf_pnts{$1} = $clan_member_ctf_pnts{$1} + $2;
        }
        else
        {
            $clan_member_ctf_pnts{$1} = $2;
        }
    
        #CTF - single user stats kills
        if (exists $clan_member_ctf_kills{$1})
        {
            $clan_member_ctf_kills{$1} = $clan_member_ctf_kills{$1} + $3;
        }
        else
        {
            $clan_member_ctf_kills{$1} = $3;
        }
    
        #CTF - single user stats dths
        if (exists $clan_member_ctf_dths{$1})
        {
            $clan_member_ctf_dths{$1} = $clan_member_ctf_dths{$1} + $4;
        }
        else
        {
            $clan_member_ctf_dths{$1} = $4;
        }
    }
    #DM: frags: 0    kills: 0    dths: 0
    elsif ($line =~ /^$clan_tag(.+).+frags: (\d+).+kills: (\d+).+dths: (\d+)/) #whitespaces fix later
    {
        $res_clan_dm_frags = $res_clan_dm_frags + $2;
        $res_clan_dm_kills = $res_clan_dm_kills + $3;
        $res_clan_dm_dths = $res_clan_dm_dths + $4;
    
        #DM - single user stats frags
        if (exists $clan_member_dm_frags{$1})
        {
            $clan_member_dm_frags{$1} = $clan_member_dm_frags{$1} + $2;
        }
        else
        {
            $clan_member_dm_frags{$1} = $2;
        }
    
        #DM - single user stats kills
        if (exists $clan_member_dm_kills{$1})
        {
            $clan_member_dm_kills{$1} = $clan_member_dm_kills{$1} + $3;
        }
        else
        {
            $clan_member_dm_kills{$1} = $3;
        }
    
        #DM - single user stats dths
        if (exists $clan_member_dm_dths{$1})
        {
            $clan_member_dm_dths{$1} = $clan_member_dm_dths{$1} + $4;
        }
        else
        {
            $clan_member_dm_dths{$1} = $4;
        }
    }
    #ATG: kills: 0    dths:0
    elsif ($line =~ /^$clan_tag(.+).+kills: (\d+).+dths: (\d+)/) #whitespaces fix later
    {
        $res_clan_atg_kills = $res_clan_atg_kills + $2;
        $res_clan_atg_dths = $res_clan_atg_dths + $3;
    
        #ATG - single user stats kills
        if (exists $clan_member_atg_kill{$1})
        {
            $clan_member_atg_kill{$1} = $clan_member_atg_kill{$1} + $2;
        }
        else
        {
            $clan_member_atg_kill{$1} = $2;
        }
    
        #ATG - single user stats dths
        if (exists $clan_member_atg_dths{$1})
        {
            $clan_member_atg_dths{$1} = $clan_member_atg_dths{$1} + $3;
        }
        else
        {
            $clan_member_atg_dths{$1} = $3;
        }   
    
    }
    # map + game type
    if ($line =~ /^Map: (.+)(ATG|CTF|DM)/)
    {
        $check++;
        $map = $1;
        $gt = $2;
    
        #how often a game type played
        if ($gt eq 'ATG')
        {
            $res_gt_atg++;
        }
        elsif ($gt eq 'CTF')
        {
            $res_gt_ctf++;
        }
        elsif ($gt eq 'DM')
        {
            $res_gt_dm++;
        }
    
        #how often this maps
        if ($map eq 'Arroyo')
        {
            $res_map_arroyo++;
        }
        elsif ($map eq 'Bahnar')
        {
            $res_map_bahnar++;
        }
        elsif ($map eq 'DustWorld')
        {
            $res_map_dustWorld++;
        }
        elsif ($map eq 'Frontier')
        {
            $res_map_frontier++;
        }
        elsif ($map eq 'HalongPort')
        {
            $res_map_halongPort++;
        }  
        elsif ($map eq 'IndianCountry')
        {
            $res_map_indiancountry++;
        }  
        elsif ($map eq 'NVABase')
        {
            $res_map_nva++;
        }  
        elsif ($map eq 'RiverDale')
        {
            $res_map_riverDale++;
        }
        elsif ($map eq 'Storm')
        {
            $res_map_storm++;
        }
        elsif ($map eq 'Stream')
        {
            $res_map_stream++;
        }
        elsif ($map eq 'ThreeCanyons')
        {
            $res_map_threeCanyons++;
        }
        elsif ($map eq 'WaterFall')
        {
            $res_map_waterFall++;
        }
    
    }
    
    # end result
    if ($line =~ /(US Army|Vietcong) points: (\d)+/)
    {
        $party = $1;
        $points = $2;
    
        #how much points VC and US
        if ($party eq 'Vietcong')
        {
            $res_points_vc = $res_points_vc + $points;
        }
        elsif ($party eq 'US Army')
        {
            $res_points_us = $res_points_us + $points;
        }
    }


}

print "\n++++++++++++++++++++++++++++++\n";
print ">> User Stats";
print "\n++++++++++++++++++++++++++++++\n";

print "ATG - kills: \n";
hashMembers(%clan_member_atg_kill); 
print "\n\n";

print "ATG - deaths: \n";
hashMembers(%clan_member_atg_dths);
print "\n\n";

print "DM - frags: \n";
hashMembers(%clan_member_dm_frags); 
print "\n\n";

print "DM - kills: \n";
hashMembers(%clan_member_dm_kills); 
print "\n\n";

print "DM - deaths: \n";
hashMembers(%clan_member_dm_dths); 
print "\n\n";

print "CTF - points: \n";
hashMembers(%clan_member_ctf_pnts); 
print "\n\n";

print "CTF - kills: \n";
hashMembers(%clan_member_ctf_kills);
print "\n\n";

print "CTF - deaths: \n";
hashMembers(%clan_member_ctf_dths);


print "\n++++++++++++++++++++++++++++++\n";
print ">> Clan Stats";
print "\n++++++++++++++++++++++++++++++\n\n";

print "ATG: \n";
print "- kills: ".$res_clan_atg_kills."\n";
print "- deaths: ".$res_clan_atg_dths."\n";

print "\nCTF: \n";
print "- points: ".$res_clan_ctf_pnts."\n";
print "- kills: ".$res_clan_ctf_kills."\n";
print "- deaths: ".$res_clan_ctf_dths."\n";

print "\nDM: \n";
print "- frags: ".$res_clan_dm_frags."\n";
print "- kills: ".$res_clan_dm_kills."\n";
print "- deaths: ".$res_clan_dm_dths."\n";

print "\n++++++++++++++++++++++++++++++\n";
print ">> Other Stats";
print "\n++++++++++++++++++++++++++++++\n\n";
print "maps \n";
print "- Arroyo: ".$res_map_arroyo."\n";
print "- Bahnar: ".$res_map_bahnar."\n";
print "- Dustworld: ".$res_map_dustWorld."\n";
print "- Frontier: ".$res_map_frontier."\n";
print "- Halongport: ".$res_map_halongPort."\n";
print "- IndianCountry: ".$res_map_indiancountry."\n";
print "- Nva: ".$res_map_nva."\n";
print "- RiverDale: ".$res_map_riverDale."\n";
print "- Storm: ".$res_map_storm."\n";
print "- Stream: ".$res_map_stream."\n";
print "- ThreeCanyons: ".$res_map_threeCanyons."\n";
print "- Waterfall: ".$res_map_waterFall."\n";

print "\ngame types \n";
print "- ATG: ".$res_gt_atg."\n";
print "- CTF: ".$res_gt_ctf."\n";
print "- DM: ".$res_gt_dm."\n";

print "\nall points \n";
print "- US Points: ".$res_points_us."\n";
print "- VC Points: ".$res_points_vc."\n";

print "\n++++++++++++++++++++++++++++++\n";
print ">> i parse $check files\n";
